package org.example.smartbuy.controller;

import org.example.smartbuy.entity.Reviews;
import org.example.smartbuy.service.IReviewsService;
import org.example.smartbuy.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reviews")
@Tag(name = "Reviews Controller", description = "Operations related to product reviews")
public class ReviewsController {

    @Autowired
    private IReviewsService reviewsService;

    /**
     * 获取所有产品评论
     * @return Result
     */
    @GetMapping("/list")
    @Operation(summary = "Get all reviews", description = "Fetch a list of all product reviews",
            responses = {
                    @ApiResponse(responseCode = "200", description = "Successfully retrieved reviews"),
                    @ApiResponse(responseCode = "400", description = "internal server error")
            })
    public Result listAllReviews(int id) {
        List<Reviews> reviews = reviewsService.listById(id);
        return Result.success(reviews);
    }

    /**
     * 根据ID查询单个评论
     * @param id 评论ID
     * @return Result
     */
    @GetMapping("/{id}")
    @Operation(summary = "Get a review by ID", description = "Fetch a review by its ID",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result getReviewById(@PathVariable int id) {
        return Result.success(reviewsService.getById(id));
    }

    /**
     * 添加新评论
     * @param reviews 评论对象
     * @return Result
     */
    @PostMapping("/add")
    @Operation(summary = "Add a new review", description = "Create a new review for a product",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result addReview(@RequestBody Reviews reviews) {
        return Result.success(reviewsService.add(reviews)) ;
    }

    /**
     * 删除评论
     * @param id 评论ID
     * @return Result
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "Delete a review", description = "Delete a review by its ID",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result deleteReview(@PathVariable int id) {
        reviewsService.delete(id);
        return Result.success();
    }
}
